import time

from selenium.webdriver.common.by import By
from common.common import driver


# 帖子编辑测试
class TestEditing:
    url=""
    driver=""

    def __init__(self):
        self.driver=driver.driver
        self.url="http://127.0.0.1:58080/index.html#"
        self.driver.get(self.url)

    # 正确发布帖子
    def Sucpush_post(self):
        # 点击主页的发布帖子
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        # 找到发新帖标题即跳转成功
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-body > div > div > div.card-body > div:nth-child(2) > label > strong").text
        assert text == "标题"
        # 添加屏幕截图
        driver.getScreeShot()

        # 正确发布帖子
        # 选择板块发布帖子，帖子是否在对应板块内，这里默认为java板块
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR, "#edit-article > div.editormd-toolbar > div > ul > li:nth-child(4) > a > i").click()
        # 滚动到页面最底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(5)
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit").click()
        # 出现成功弹窗即发布成功
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text == "成功"
        # 添加屏幕截图
        driver.getScreeShot()

        # 检查帖子标题是否正确
        text=self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong").text
        assert text=="1"

        # 检查帖子发布板块是否正确
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(2) > a").click()
        # 添加屏幕截图
        driver.getScreeShot()

        # 回到主页
        self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a").click()

    # 错误发布帖子
    def Failpush_post(self):
        # 点击主页的发布帖子
        self.driver.find_element(By.CSS_SELECTOR, "#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        # 找到标题即跳转成功
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR, "#bit-forum-content > div.page-body > div > div > div.card-body > div:nth-child(2) > label > strong").text
        assert text=="标题"
        # 添加屏幕截图
        driver.getScreeShot()

        # 错误发布帖子
        # 不输入标题和内容，点击发布
        # 滚动到页面最底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit").click()
        # 出现提示弹窗即发布失败
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="提示"
        # 添加屏幕截图
        driver.getScreeShot()

        # 只输入标题，点击发布
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_title").send_keys("1")
        # 滚动到页面最底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit").click()
        # 出现提示弹窗即发布失败
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text == "提示"
        # 添加屏幕截图
        driver.getScreeShot()

        # 只输入内容，点击发布
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#edit-article > div.editormd-toolbar > div > ul > li:nth-child(4) > a > i").click()
        # 滚动到页面最底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit").click()
        # 出现提示弹窗即发布失败
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text == "提示"
        # 添加屏幕截图
        driver.getScreeShot()

        # 滚动到顶部
        #driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        #time.sleep(3)

        # 回到主页
        self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a").click()

    # 正确修改帖子
    def Sucedit_post(self):
        # 正确修改帖子，点击提交
        # 点击帖子标题进入帖子详情页
        self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a").click()
        # 页面找到标题即跳转成功
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR, "#details_article_title").text
        assert text == "1"

        # 点击编辑按钮
        self.driver.find_element(By.CSS_SELECTOR, "#details_artile_edit").click()
        # 若找到编辑标题即跳转成功
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR, "#bit-forum-content > div.page-header.d-print-none > div > div > div > h2").text
        assert text=="编辑"
        # 添加屏幕截图
        driver.getScreeShot()

        # 编辑标题
        self.driver.find_element(By.CSS_SELECTOR, "#edit_article_title").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#edit_article_title").send_keys("11")
        # 点击提交
        # 滚动到页面最底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "#edit_article_submit").click()
        # 弹出成功弹窗即修改成功
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="成功"
        # 添加屏幕截图
        driver.getScreeShot()
        # 帖子标题是否更新
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong").text
        assert text=="11"

    # 错误修改帖子
    def Failedit_post(self):
        # 点击帖子标题进入帖子详情页
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a").click()
        # 页面找到标题即跳转成功
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR, "#details_article_title").text
        assert text == "1"
        # 点击编辑按钮
        self.driver.find_element(By.CSS_SELECTOR, "#details_artile_edit").click()
        # 若找到编辑标题即跳转成功
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div > h2").text
        assert text == "编辑"
        # 添加屏幕截图
        driver.getScreeShot()

        # 只输入内容，点击提交
        # 清空标题,只输入内容
        self.driver.find_element(By.CSS_SELECTOR, "#edit_article_title").clear()
        # 点击提交
        # 滚动到页面最底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "#edit_article_submit").click()
        # 弹出提示弹窗即修改失败
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text == "提示"
        # 添加屏幕截图
        driver.getScreeShot()

        # 回到主页
        self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a").click()

    # 删除帖子
    def Delete_post(self):
        # 点击帖子标题进入帖子详情页
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a").click()
        # 页面找到标题即跳转成功
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR, "#details_article_title").text
        assert text == "11"
        # 点击删除按钮
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a").click()
        # 弹出警告提示弹窗
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR, "#details_delete_modal > div > div > div.modal-body.text-center.py-4 > h3").text
        assert text=="确定要删除当前帖子吗?"
        # 添加屏幕截图
        driver.getScreeShot()
        # 点击删除键
        self.driver.find_element(By.CSS_SELECTOR, "#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(2)").click()
        # 弹出成功弹窗即删除成功
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="成功"
        # 自动跳转主页，找到主页标题即删除成功
        time.sleep(1)
        text=self.driver.find_element(By.CSS_SELECTOR,"#article_list_board_title").text
        assert text=="首页"
        # 添加屏幕截图
        driver.getScreeShot()

